<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/28
 * Time: 14:06
 * 储物柜管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;

class Cabinet extends OperationBase {

	function _initialize() {
		parent::_initialize();  
    }


	function index(){
        $auth_group = sp_get_auth_menu_3("20051000");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
		$this->assign("now", date("Y-m-d"));
		echo $this->fetch();
	}

    /**
     * 列表数据
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
	function ajaxIndex(){

	    $cabinet_no = input("cabinet_no/s");
	    $card_no = input("card_no/s");
        $start = input("start/s");
        $end = input("end/s");
        $draw = input("draw",0) + 1;//请求时间
        $shop_id = input("shop_id/d");

        $where = array();
        if (empty($start)) {
            $start = date('Y-m-d') . " 00:00:00";
        } else {
            $start = $start . " 00:00:00";
        }
        if (empty($end)) {
            $end = date('Y-m-d') . " 23:59:59";
        }else{
            $end = $end . " 23:59:59";
        }
        if ($end < $start) {
            $this->error("起始时间不能大于结束时间");
        }
        $where["c.create_time"] = array("between time", array($start, $end));
        if (!empty($cabinet_no)) {
            $where["c.cabinet_no"] = array("like", "%{$cabinet_no}%");
        }
        if (!empty($card_no)) {
            $where["c.card_no"] = array("like", "%{$card_no}%");
        }
        if(!empty($shop_id)){
            $where["c.shop_id"] = $shop_id;
        }

        $total = db("cabinet")->alias("c")->where($where)->count(1);
        $list = db("cabinet")->alias("c")
            ->field("c.*, s.shop_name")
            ->join("m_shop s", "s.id = c.shop_id", "left")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();

        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
		$this->ajax($data);
	}

	/**
     * 租用柜
     */
	public function rent()
    {
        $auth_group = sp_get_auth_menu_3("20051001");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        $this->assign("now", date("Y-m-d"));
        echo $this->fetch();
    }

    /**
     * 列表数据
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    function ajaxRent(){

        $rent_no = input("rent_no/s");
        $card_no = input("card_no/s");
        $shop_id = input("shop_id/d");
        $start = input("start/s");
        $end = input("end/s");
        $draw = input("draw",0) + 1;//请求时间

        $where = array();
        if (!empty($rent_no)) {
            $where["c.rent_no"] = array("like", "%{$rent_no}%");
        }
        if (!empty($card_no)) {
            $where["c.card_no"] = array("like", "%{$card_no}%");
        }
        if(!empty($shop_id)){
            $where["c.shop_id"] = $shop_id;
        }

        if (!empty($end) && !empty($start)) {
            if ($end < $start) {
                $this->error("起始时间不能大于结束时间");
            }
        }
        if (!empty($start)) {
            $where["c.start_date"] = array("gt", strtotime($start));
        }
        if (!empty($end)) {
            $where["c.end_date"] = array("gt", strtotime($end));
        }

        $total = db("rent_cabinet")->alias("c")->where($where)->count(1);
        $list = db("rent_cabinet")->alias("c")
            ->field("c.*, s.shop_name")
            ->join("m_shop s", "s.id = c.shop_id", "left")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();

        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }
}
